#pragma once

#include <iosfwd>
#include <sstream>

#include "json/rapidjson.h"
#include "json/document.h"
#include "base/CCConsole.h"
#include "platform/CCFileUtils.h"

class CCocosUtil
{
public:
	CCocosUtil(void);
	~CCocosUtil(void);
};

class JsonMap {
public:
	JsonMap(const std::string& name) : m_name(name) {}
	~JsonMap() {}

	template<typename K, typename V, typename M>
	void Parse(M& m_) {
		rapidjson::Document _doc; 
		do 
		{
			std::string pBytes = cocos2d::FileUtils::getInstance()->getStringFromFile(m_name);
			_doc.Parse<0>(pBytes.c_str());
			CC_BREAK_IF(_doc.HasParseError());
			CC_BREAK_IF(!_doc.IsObject());
			CC_BREAK_IF(!_doc.HasMember("root"));
			const rapidjson::Value &pArray = _doc["root"];
			CC_BREAK_IF(!pArray.IsObject());

			for (auto it = pArray.MemberonBegin(); it != pArray.MemberonEnd(); ++it) {
				std::istringstream tStreamK;
				std::istringstream tStreamV;
				tStreamK.str(it->name.GetString());
				tStreamV.str(it->value.GetString());
				K tKey;
				tStreamK >> tKey;
				V tValue;
				tStreamV >> tValue;
				m_[tKey] = tValue;
			}

		} while (0);
	}
private:
	std::string			m_name;
};

#ifdef WIN32
const char* GBKToUTF8(const char *strChar);
#define UTEXT(str) GBKToUTF8(str)
#else
#define UTEXT(str) str
#endif
